package cn.handong.tool.Java文件操作.excel;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;
//import com.sgm.b2c.mmc.common.config.properties.MqProperties;
//import com.sgm.b2c.mmc.common.enums.DomainEnum;
//import com.sgm.b2c.mmc.mq.MqProducer;
//import com.sgm.b2c.mmc.mq.model.SyncCardnumModel;
//import org.apache.log4j.Logger;
//import org.junit.Test;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.ActiveProfiles;
//import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 补发雪佛兰粉丝转会员消息
 **/
//@ActiveProfiles(value = "qa")
//@RunWith(SpringRunner.class)
//@SpringBootTest
public class ReadExcelTest {

//    private static Logger logger = Logger.getLogger(SelectHouse.class.toString());
//    @Autowired
//    private MqProperties mqProperties;
//    @Autowired
//    private MqProducer mqProducer;
    /**
     * 发消息
     */
    @Test
    public void synchronousRead1() {
        long millis1 = System.currentTimeMillis();
        String fileName = "C:\\Users\\sqkb8v\\Desktop\\chevy--.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 同步读取会自动finish
        List<SgmData> list = EasyExcel.read(fileName)
                .head(SgmData.class)
                .sheet()
                .headRowNumber(1)
                .doReadSync();
        for(SgmData li:list){
            System.out.println(li.toString());
//            DomainEnum domainEnum = DomainEnum.getByDomainCode("3");//雪佛兰 3
//            String topicAndTag = mqProperties.getSyncCardnum();
//            if (domainEnum != null) {
//                topicAndTag = topicAndTag + ":" + domainEnum.name();
//            }
//            SyncCardnumModel syncCardnumModel = new SyncCardnumModel();
//            syncCardnumModel.setIdpUserId(li.getIdpUserId());
//            syncCardnumModel.setDomain("3"); //雪佛兰 3
//            syncCardnumModel.setCardNumber(li.getCardNumber());
//            syncCardnumModel.setMemberType(li.getMemberType());
//            mqProducer.sendMqMessage(syncCardnumModel, topicAndTag, li.getCardNumber());
//            System.out.println(li);
        }
        long millis2 = System.currentTimeMillis();
        long time=millis2-millis1;//经过的毫秒数
        System.out.println(time+"毫秒");
    }


    //中建熙江岳
    @Test
    public void synchronousRead2() {
        String fileName = "xjy1.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 同步读取会自动finish
        List<HouseData> list = EasyExcel.read(fileName)
                .head(HouseData.class)
                .sheet()
                .headRowNumber(1)
                .doReadSync();
        List<HouseData> lists = list.stream().filter(
                item -> item.getTotalPrice() <= 4600000 //总价低于460w
//                        && (item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '1'
//                        || item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '4')
                        && item.getMeasure() >= 90     //大于等于90平
                        &&
                        item.getFlourNumber() >= 3  //大于等于3楼
                        &&
                        item.getBuildingNumber() == 12
//                (item.getBuildingNumber() == 6 && item.getFlourNumber() >= 3) ||
//                (item.getBuildingNumber() != 6 && item.getFlourNumber() >= 5))
        ).sorted().collect(Collectors.toList());

        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        for (HouseData houseData : lists) {
            messageList.add(houseData.getBuildingNumber() + "-" + houseData.getRoomNumber() + "-" + houseData.getTotalPrice());
        }
        try {
            writeToTxt(folder, file, messageList);
        } catch (FileNotFoundException e) {
//            logger.error("Error Message :", e);
        }
//
//        for(HouseData li:lists){
//            System.out.println(li.toString());
//        }
    }

    /**
     * 逐行读取TXT
     */
    @Test
    public void readTxtToObject() {
        try {
            String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
            String path = "test.txt";
            BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(path)), StandardCharsets.UTF_8));
            String lineTxt;
            // 逐行读取
            while ((lineTxt = br.readLine()) != null) {
                String[] split = lineTxt.split("-");
                Integer price = Integer.parseInt(split[2]);
                Integer bn = Integer.parseInt(split[0]);
                Integer floorNum = Integer.parseInt(split[1].substring(0,split[1].length() - 2));
                System.out.println(lineTxt);
            }
            br.close();
        } catch (Exception e) {
//            logger.error("Error Message :"+ e);
        }
    }


    /**
     * 写入txt
     */
    @Test
    public void testWriteToTxt() {
        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        messageList.add("ok,0");
        messageList.add("no,1");
        messageList.add("pass,2");
        messageList.add("okay,3");

        try {
            writeToTxt(folder, file, messageList);
        } catch (FileNotFoundException e) {
//            logger.error("Error Message :", e);
        }

    }

    /**
     * 方法1写入txt
     *
     //     * @param filePath
     * @param message
     * @throws IOException
     */
    public void writetoTxt1(String folder, String filename, String message) throws IOException {
        File files = new File(folder);
        files.mkdirs();
        File file = new File(folder + File.separator + filename);
        file.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(message);
        out.flush();
    }






    /**
     * 方法2写入txt
     *
     //     * @param foler
     * @param file
     * @param messageList
     * @throws FileNotFoundException
     */
    public void writeToTxt(String folder, String file, List<String> messageList) throws FileNotFoundException {
        File files = new File(folder);
        files.mkdirs();

        FileOutputStream fos = new FileOutputStream(folder + File.separator + file);

        // 逐行写入
        PrintWriter pw = new PrintWriter(fos);
        for (String message : messageList) {
            pw.println(message);
        }
        pw.close();

    }

    /**
     * 测试方法2写入txt
     */
    @Test
    public void writeToTxt() {

//        String folder = "C:\\Test0";
        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String filename = "000.txt";

        String message = "this is message \n";
        for (int i = 0; i < 3; i++) {
            message += "abc \n";
        }
        try {
            writetoTxt1(folder, filename, message);
        } catch (IOException e) {
//            logger.error("Error Message :", e);
        }

    }




    @Test
    public void te(){
        String str = "213";
        System.out.println(str.charAt(str.length() - 1) == '1');
    }
}